<?php
/**
 * pagination
 */
class PagerHelper
{
	static function split($count, $pagesize=null, $url_param=array())
	{
		if(isprint()){
			return array('r_offset'=>0, 'p_size'=>0);
		}
		
		$tmp		= array();
		$tmp['key']	= Core::$convent['page_key'];

		$page		= fetch_a_int($tmp['key']) ?: 1;
		if($page <1) $page=1;
		if(!$pagesize) $pagesize=Core::$convent['page_size'];
		
		$tmp['p_total']		= ceil($count/$pagesize);
		$tmp['p_offset']	= $page>$tmp['p_total']?$tmp['p_total']:$page;
		$tmp['p_size']		= $pagesize;
		$tmp['r_total']		= $count;
		$tmp['r_offset']	= $tmp['p_offset']?$pagesize * ($tmp['p_offset']-1):0;
		
		$url_param		= array_diff_key(array_merge($_GET, $_POST, $url_param), array($tmp['key']=>null));
		if($url_param) {
			$u	= array();
			foreach($url_param as $k=>$v) {
				$u[]	= $k.'/'.urlencode(urldecode($v));
			}
			$tmp['url']	= url($_REQUEST['a'].'/'.join('/',$u)."/{$tmp['key']}/{PAGE}");
			unset($u);
		}
		else {
			$tmp['url']	= url($_REQUEST['a']."/{$tmp['key']}/{PAGE}");
		}

		View::assign('pager',	$tmp);
		return $tmp;
	}
	
	static function makeLimit($pager){
		return array($pager['r_offset'], $pager['p_size']);
	}

	static function split2limit($count, $pagesize=null, $url_param=array()){
		return self::makeLimit(self::split($count, $pagesize, $url_param));
	}

	static function show($pager){
		return Html::pager($pager);
	}

}
?>
